<?php

namespace app\common\library;

class Common
{
    public static function getAntiStealingLink($url, $key)
    {
        $query = [];
        $query['KEY'] = $key;
        $query['Dir'] = dirname(parse_url($url)['path']) . '/';
        $query['t'] = dechex(time() + 60 * 20);
        $query['us'] = self::getRandStr(10);
        $query['sign'] = md5($query['KEY'] . $query['Dir'] . $query['t'] . $query['us']);
        unset($query['KEY'], $query['Dir']);
        return $url . '?' . http_build_query($query);
    }

    public static function getRandStr($length)
    {
        $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $len = strlen($str) - 1;
        $randStr = '';
        for ($i = 0; $i < $length; $i++) {
            $index = mt_rand(0, $len);
            $randStr .= $str[$index];
        }
        return $randStr;
    }

}